package com.zenithmind.task.controller;

import com.zenithmind.common.context.UserContext;
import com.zenithmind.common.result.PageResult;
import com.zenithmind.common.result.Result;
import com.zenithmind.task.pojo.entity.TaskFollower;
import com.zenithmind.task.pojo.query.TaskFollowerQuery;
import com.zenithmind.task.pojo.vo.TaskFollowerVO;
import com.zenithmind.task.service.TaskFollowerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 任务关注管理控制器
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Slf4j
@RestController
@RequestMapping("/api/zenithMind/task-follower")
@RequiredArgsConstructor
@Tag(name = "任务关注管理", description = "任务关注的创建、查询、取消等功能")
public class TaskFollowerController {

    private final TaskFollowerService taskFollowerService;

    // ==================== 基础CRUD操作 ====================

    @GetMapping("/page")
    @Operation(summary = "分页查询任务关注列表", description = "支持多条件筛选的任务关注分页查询")
    public Result<PageResult<TaskFollowerVO>> getTaskFollowerPage(TaskFollowerQuery query) {
        String userId = UserContext.requireUserId();
        PageResult<TaskFollowerVO> result = taskFollowerService.getTaskFollowerPage(query, userId);
        return Result.success(result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "查询任务关注详情", description = "根据ID查询任务关注的详细信息")
    public Result<TaskFollowerVO> getTaskFollowerById(@Parameter(description = "关注ID") @PathVariable String id) {
        String userId = UserContext.requireUserId();
        TaskFollowerVO taskFollower = taskFollowerService.getTaskFollowerById(id, userId);
        return Result.success(taskFollower);
    }

    @PostMapping
    @Operation(summary = "创建任务关注", description = "创建新的任务关注记录")
    public Result<Boolean> createTaskFollower(@Valid @RequestBody TaskFollower taskFollower) {
        String userId = UserContext.requireUserId();
        Boolean result = taskFollowerService.createTaskFollower(taskFollower, userId);
        return Result.success(result);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除任务关注", description = "删除指定的任务关注记录")
    public Result<Boolean> deleteTaskFollower(@Parameter(description = "关注ID") @PathVariable String id) {
        String userId = UserContext.requireUserId();
        Boolean result = taskFollowerService.deleteTaskFollower(id, userId);
        return Result.success(result);
    }

    // ==================== 关注操作 ====================

    @PostMapping("/follow/{taskId}")
    @Operation(summary = "关注任务", description = "关注指定的任务")
    public Result<Boolean> followTask(@Parameter(description = "任务ID") @PathVariable String taskId) {
        String userId = UserContext.requireUserId();
        Boolean result = taskFollowerService.followTask(taskId, userId);
        return Result.success(result);
    }

    @DeleteMapping("/unfollow/{taskId}")
    @Operation(summary = "取消关注任务", description = "取消关注指定的任务")
    public Result<Boolean> unfollowTask(@Parameter(description = "任务ID") @PathVariable String taskId) {
        String userId = UserContext.requireUserId();
        Boolean result = taskFollowerService.unfollowTask(taskId, userId);
        return Result.success(result);
    }

    @PostMapping("/follow/batch")
    @Operation(summary = "批量关注任务", description = "批量关注多个任务")
    public Result<Boolean> batchFollowTasks(@RequestBody List<String> taskIds) {
        String userId = UserContext.requireUserId();
        Boolean result = taskFollowerService.batchFollowTasks(taskIds, userId);
        return Result.success(result);
    }

    @DeleteMapping("/unfollow/batch")
    @Operation(summary = "批量取消关注任务", description = "批量取消关注多个任务")
    public Result<Boolean> batchUnfollowTasks(@RequestBody List<String> taskIds) {
        String userId = UserContext.requireUserId();
        Boolean result = taskFollowerService.batchUnfollowTasks(taskIds, userId);
        return Result.success(result);
    }

    // ==================== 查询方法 ====================

    @GetMapping("/task/{taskId}/followers")
    @Operation(summary = "查询任务的关注者列表", description = "查询指定任务的所有关注者")
    public Result<List<TaskFollowerVO>> getTaskFollowers(
            @Parameter(description = "任务ID") @PathVariable String taskId) {
        String userId = UserContext.requireUserId();
        List<TaskFollowerVO> result = taskFollowerService.getTaskFollowers(taskId, userId);
        return Result.success(result);
    }

    @GetMapping("/my/followed-tasks")
    @Operation(summary = "查询我关注的任务", description = "查询当前用户关注的所有任务")
    public Result<PageResult<TaskFollowerVO>> getMyFollowedTasks(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "页大小") @RequestParam(defaultValue = "10") Integer pageSize) {
        String userId = UserContext.requireUserId();
        PageResult<TaskFollowerVO> result = taskFollowerService.getUserFollowedTasks(userId, pageNum, pageSize);
        return Result.success(result);
    }

    @GetMapping("/check/{taskId}")
    @Operation(summary = "检查是否已关注任务", description = "检查当前用户是否已关注指定任务")
    public Result<Boolean> isFollowing(@Parameter(description = "任务ID") @PathVariable String taskId) {
        String userId = UserContext.requireUserId();
        Boolean result = taskFollowerService.isFollowing(taskId, userId);
        return Result.success(result);
    }

    @GetMapping("/statistics")
    @Operation(summary = "获取关注统计信息", description = "获取任务关注的统计信息")
    public Result<Object> getFollowStatistics(
            @Parameter(description = "任务ID") @RequestParam(required = false) String taskId) {
        String userId = UserContext.requireUserId();
        
        if (taskId != null) {
            // 获取指定任务的关注统计
            List<TaskFollowerVO> followers = taskFollowerService.getTaskFollowers(taskId, userId);
            return Result.success(Map.of(
                "taskId", taskId,
                "followerCount", followers.size(),
                "followers", followers
            ));
        } else {
            // 获取用户的关注统计
            PageResult<TaskFollowerVO> followedTasks = taskFollowerService.getUserFollowedTasks(userId, 1, Integer.MAX_VALUE);
            return Result.success(Map.of(
                "userId", userId,
                "followedTaskCount", followedTasks.getTotal(),
                "followedTasks", followedTasks.getRecords()
            ));
        }
    }

    @GetMapping("/recent")
    @Operation(summary = "获取最近关注的任务", description = "获取当前用户最近关注的任务列表")
    public Result<List<TaskFollowerVO>> getRecentFollowedTasks(
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "10") Integer limit) {
        String userId = UserContext.requireUserId();
        PageResult<TaskFollowerVO> result = taskFollowerService.getUserFollowedTasks(userId, 1, limit);
        return Result.success(result.getRecords());
    }
}
